inject.js ➔ ... ➔ ???   B
last analyzed

Complexity

Conditions 7
Paths 12

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 12
nop 1
dl 0
loc 19
rs 8
c 0
b 0
f 0
1
(() => {
2
	try {
3
    const detectJs = chain => {
4
      const properties = chain.split('.');
5
6
      let value = properties.length ? window : null;
7
8
      for ( let i = 0; i < properties.length; i ++ ) {
9
        let property = properties[i];
10
11
        if ( value && value.hasOwnProperty(property) ) {
12
          value = value[property];
13
        } else {
14
          value = null;
15
16
          break;
17
        }
18
      }
19
20
      return typeof value === 'string' || typeof value === 'number' ? value : !!value;
21
    };
22
23
    const onMessage = event => {
24
      if ( event.data.id !== 'patterns' ) {
25
        return;
26
      }
27
28
      removeEventListener('message', onMessage);
29
30
      const patterns = event.data.patterns || {};
31
32
      const js = {};
33
34
      for ( let appName in patterns ) {
35
        if ( patterns.hasOwnProperty(appName) ) {
36
          js[appName] = {};
37
38
          for ( let chain in patterns[appName] ) {
39
            if ( patterns[appName].hasOwnProperty(chain) ) {
40
              js[appName][chain] = {};
41
42
              for ( let index in patterns[appName][chain] ) {
43
                const value = detectJs(chain);
44
45
                if ( value && patterns[appName][chain].hasOwnProperty(index) ) {
46
                  js[appName][chain][index] = value;
47
                }
48
              }
49
            }
50
          }
51
        }
52
      }
53
54
      postMessage({ id: 'js', js }, '*');
55
    };
56
57
    addEventListener('message', onMessage);
58
  } catch(e) {
59
    // Fail quietly
60
  }
61
})();
62